package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.StaffRoleDao;
import cn.ym.mall.entity.StaffRole;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StaffRoleDaoImpl implements StaffRoleDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 获得所有员工的角色编号
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<StaffRole> getAll() throws SQLException {
        String sql = "select stid, rid from ym_staffrole;";
        List<StaffRole> staffRoleList = new ArrayList<StaffRole>();
        try {
            staffRoleList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<StaffRole>(StaffRole.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return staffRoleList;
    }

    /**
     * 添加新员工角色编号
     *
     * @param staffRole
     * @return
     * @throws SQLException
     */
    @Override
    public int add(StaffRole staffRole) throws SQLException {
        String sql = "insert into ym_staffrole(stid,rid) value (?,?);";
        Object[] arr = {staffRole.getRid(),staffRole.getStid()};
        int num = 0;
        try {
            num = queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 更新员工角色编号
     *
     * @param staffRole
     * @return
     * @throws SQLException
     */
    @Override
    public int update(StaffRole staffRole) throws SQLException {
        String sql = "update ym_staffrole set rid=? where stid=?;";
        Object[] arr = {staffRole.getRid(),staffRole.getStid()};
        int num = 0;
        try {
            num = queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据员工编号，移除角色编号(根据员工编号取消员工权限)
     *
     * @param stid
     * @return
     */
    @Override
    public int delete(Integer stid) throws SQLException {
        String sql = "delete from ym_staffrole where stid=?;";
        Object[] arr = {stid};
        int num = 0;
        try {
            num = queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据角色编号查询员工编号(某个角色的所有员工的编号)
     *
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public List<StaffRole> getAllByRid(Integer rid) throws SQLException {
        String sql = "select stid, rid from ym_staffrole where rid=?;";
        List<StaffRole> staffRoleList = new ArrayList<StaffRole>();
        try {
            staffRoleList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<StaffRole>(StaffRole.class),rid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return staffRoleList;
    }
}
